package cn.ihelloworld.controller;

import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import cn.ihelloworld.bean.Article;
import cn.ihelloworld.bean.Catagory;
import cn.ihelloworld.bean.Comment;
import cn.ihelloworld.bean.PageResult;
import cn.ihelloworld.bean.Tag;
import cn.ihelloworld.service.IArticleService;
import cn.ihelloworld.service.ICatagoryService;
import cn.ihelloworld.service.ICommentService;
import cn.ihelloworld.service.ITagService;

@Controller
public class CommonController {
	
	@Autowired
	private ICatagoryService catagoryService;
	@Autowired
	private IArticleService articleService;
	@Autowired
	private ITagService tagService;
	@Autowired
	private ICommentService commentService;
	
	@RequestMapping("/")
	public ModelAndView index(){
		return index(new ModelAndView());
	}
	@RequestMapping("index.html")
	public ModelAndView index(ModelAndView view){
		List<Catagory> catagorys=catagoryService.select(null);
		for(Catagory catagory:catagorys){
			List<Article> articlesLast=articleService.selectLast(catagory.getCode(),5);
			List<Article> articlesHot=articleService.selectHot(catagory.getCode(),5);
			catagory.setArticlesLast(articlesLast);
			catagory.setArticlesHot(articlesHot);
		}
		view.addObject("catagorys", catagorys);
		List<Tag> tags=tagService.selectHot(10);
		view.addObject("tags", tags);
		view.setViewName("index");
		return view;
	}
	@RequestMapping("tag.html")
	public ModelAndView tag(String name,ModelAndView view){
//		try {
//			name=new String(name.getBytes("ISO8859-1"),"UTF-8");
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		tagService.updateReadCountByName(name);
		List<Catagory> catagorys=catagoryService.select(null);
		view.addObject("catagorys", catagorys);
		List<Tag> tags=tagService.selectHot(10);
		view.addObject("tags", tags);
		List<Article> articlesLast=articleService.selectLastByTag(name, 10);
		List<Article> articlesHot=articleService.selectHotByTag(name, 10);
		view.addObject("articlesLast", articlesLast);
		view.addObject("articlesHot", articlesHot);
		view.addObject("tag", name);
		view.setViewName("tag");
		return view;
	}
	@RequestMapping("{catagory}.html")
	public ModelAndView catagory(@PathVariable("catagory")String code,Integer page,ModelAndView view){
		Catagory catagory=new Catagory();
		catagory.setCode(code);
		List<Catagory> catagorythis=catagoryService.select(catagory);
		catagory=catagorythis.get(0);
		view.addObject("catagory", catagory);
		List<Catagory> catagorys=catagoryService.select(null);
		view.addObject("catagorys", catagorys);
		PageResult<Article> pageResult=null;
		if(page==null){
		  pageResult=articleService.selectByPage(code, 1);
		}else{
		  pageResult=articleService.selectByPage(code, page);
		}
		view.addObject("pageResult", pageResult);
		
		List<Article> articlesHot=articleService.selectHot(code,10);
		view.addObject("articlesHot", articlesHot);
		view.setViewName("catagory");
		List<Tag> tags=tagService.selectHot(10);
		view.addObject("tags", tags);
		
	//生成分页数据
    StringBuffer html=new StringBuffer();
    if(pageResult.getPageCount()==1){
      view.addObject("pagehtml", html);
      return view;
    }
    html.append("<div class=\"yellow\">");
//    if(pageResult.getCurrentPage()==1){
//      html.append("<span class=\"disabled\"> &lt; </span>");
//    }else{
//      html.append("<a href=\""+catagory.getCode()+".html?page="+(pageResult.getCurrentPage()-1)+"\">&lt;</a>");
//    }
//    if(pageResult.getPageCount()>1&&pageResult.getPageCount()<=10){
//      for(int i=1;i<=pageResult.getPageCount();i++){
//        if(pageResult.getCurrentPage()==i){
//          html.append("<span class=\"current\">"+i+"</span>");
//        }else{
//          html.append("<a href=\""+catagory.getCode()+".html?page="+i+"\">"+i+"</a>");
//        }
//      }
//    }else{
//      if(pageResult.getCurrentPage()%7==0){
//        for(int i=pageResult.getCurrentPage();i<=pageResult.getCurrentPage()+7;i++){
//          if(pageResult.getCurrentPage()==i){
//            html.append("<span class=\"current\">"+i+"</span>");
//          }else{
//            html.append("<a href=\""+catagory.getCode()+".html?page="+i+"\">"+i+"</a>");
//          }
//        }
//      }else{
//        if(pageResult.getCurrentPage()>=pageResult.getPageCount()-10){
//          for(int i=pageResult.getPageCount()-10;i<=pageResult.getPageCount();i++){
//            if(pageResult.getCurrentPage()==i){
//              html.append("<span class=\"current\">"+i+"</span>");
//            }else{
//              html.append("<a href=\""+catagory.getCode()+".html?page="+i+"\">"+i+"</a>");
//            }
//          }
//        }else{
//          for(int i=pageResult.getCurrentPage()+1-(pageResult.getCurrentPage()%7);i<=pageResult.getCurrentPage()-(pageResult.getCurrentPage()%7)+7;i++){
//            if(pageResult.getCurrentPage()==i){
//              html.append("<span class=\"current\">"+i+"</span>");
//            }else{
//              html.append("<a href=\""+catagory.getCode()+".html?page="+i+"\">"+i+"</a>");
//            }
//          }
//          html.append("<span>...</span>");
//          html.append("<a href=\""+catagory.getCode()+".html?page="+(pageResult.getPageCount()-1)+"\">"+(pageResult.getPageCount()-1)+"</a>");
//          html.append("<a href=\""+catagory.getCode()+".html?page="+pageResult.getPageCount()+"\">"+pageResult.getPageCount()+"</a>");
//        }
//        
//      }
//    }
//    if(pageResult.getCurrentPage()==pageResult.getPageCount()){
//      html.append("<span class=\"disabled\"> &gt; </span>");
//    }else{
//      html.append("<a href=\""+catagory.getCode()+".html?page="+(pageResult.getCurrentPage()+1)+"\">&gt;</a>");
//    }
    if(pageResult.getCurrentPage()<=10){
      if(pageResult.getCurrentPage()==1){
        html.append("<span class=\"disabled\"> &lt;&lt; </span>");
      }else{
        html.append("<a href=\""+catagory.getCode()+".html?page=1\">&lt;&lt;</a>");
      }
      html.append("<span class=\"disabled\"> &lt; </span>");
    }else{
      html.append("<a href=\""+catagory.getCode()+".html?page=1\">&lt;&lt;</a>");
      html.append("<a href=\""+catagory.getCode()+".html?page="+(pageResult.getCurrentPage()-10)+"\">&lt;</a>");
    }
    int start=pageResult.getCurrentPage()/10*10+1;
    int end=pageResult.getCurrentPage()/10*10+10;
    for(int i=start;i<=end;i++){
      if(i<=pageResult.getPageCount()){
        if(pageResult.getCurrentPage()==i){
          html.append("<span class=\"current\">"+i+"</span>");
        }else{
          html.append("<a href=\""+catagory.getCode()+".html?page="+i+"\">"+i+"</a>");
        }
      }
    }
    
    if(pageResult.getCurrentPage()/10==pageResult.getPageCount()/10&&pageResult.getCurrentPage()%10!=0){
      html.append("<span class=\"disabled\"> &gt; </span>");
      if(pageResult.getCurrentPage()==pageResult.getPageCount()){
        html.append("<span class=\"disabled\"> &gt;&gt; </span>");
      }else{
        html.append("<a href=\""+catagory.getCode()+".html?page="+pageResult.getPageCount()+"\">&gt;&gt;</a>");
      }
    }else{
      if(pageResult.getCurrentPage()+10<=pageResult.getPageCount()){
        html.append("<a href=\""+catagory.getCode()+".html?page="+(pageResult.getCurrentPage()+10)+"\">&gt;</a>");
      }else{
        html.append("<a href=\""+catagory.getCode()+".html?page="+pageResult.getPageCount()+"\">&gt;</a>");
      }
      html.append("<a href=\""+catagory.getCode()+".html?page="+pageResult.getPageCount()+"\">&gt;&gt;</a>");
    }  
    
    html.append("</div>");
    view.addObject("pagehtml", html);
		return view;
	}
	@RequestMapping("{catagory}/{article}.html")
	public ModelAndView article(@PathVariable("catagory")String catagoryCode,@PathVariable("article")String articleId,ModelAndView view){
		Logger.getLogger(getClass()).debug("catagory="+catagoryCode+";article="+articleId);
		List<Catagory> catagorys=catagoryService.select(null);
		view.addObject("catagorys", catagorys);
		Article article=articleService.get(articleId);
		article.setReadCount(article.getReadCount()+1);
		articleService.saveOrUpdate(article);
		view.addObject("article", article);
		List<Tag> tags=tagService.selectHot(10);
		view.addObject("tags", tags);
		List<Article> articlesHot=articleService.selectHot(catagoryCode,5);
		view.addObject("articlesHot", articlesHot);
		Comment comment=new Comment();
		comment.setArticleId(articleId);
		Article a2=new Article();
		a2.setId(articleId);
		comment.setArticle(a2);
		List<Comment> comments=commentService.selectForPage(comment, 1, 15).getRs();
		view.addObject("comments", comments);
		List<Article> preNext=articleService.selectPreNext(articleId);
		view.addObject("preNext", preNext);
		view.setViewName("article");
		return view;
	}
	@RequestMapping("commentadd.html")
	public ModelAndView commentAdd(String articleId,String content,String catagoryCode){
		Comment comment=new Comment();
		Article article=articleService.get(articleId);
		article.setCommentCount(article.getCommentCount()+1);
		articleService.saveOrUpdate(article);
		comment.setArticle(article);
		content=content.replaceAll("<", "&lt;");
		content=content.replaceAll(">", "&gt;");
		comment.setContent(content);
		comment.setCreateDate(new Date());
		commentService.saveOrUpdate(comment);
		return new ModelAndView(new RedirectView(catagoryCode+"/"+articleId+".html"));
	}
}
